package com.wxzz.elearing.comment.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.CommunityAnswersDescDto;
import com.wxzz.elearing.comment.dto.SkillCommentDto;
import com.wxzz.elearing.comment.dto.SkillDto;
import com.wxzz.elearing.comment.dto.SkillPublishDto;
import com.wxzz.elearing.comment.entity.CommunityComment;
import com.wxzz.elearing.comment.entity.Skill;
import com.wxzz.elearing.comment.entity.SkillComment;
import com.wxzz.elearing.comment.service.ICommunityCommentService;
import com.wxzz.elearing.comment.service.ISkillService;
import com.wxzz.elearing.comment.vo.SkillPublishVo;
import com.wxzz.elearing.comment.vo.SkillVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 技能 前端控制器(wx)
 * </p>
 *
 * @author
 * @since 2021-05-17
 */
@RestController
@RequestMapping("/wx-skill")
@Api(tags = "技能接口(小程序)")
public class WxSkillController {

    @Autowired
    private ISkillService skillService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "新增技能(小程序)", notes = "新增或修改技能(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveSkill", method = RequestMethod.POST)
    public ReturnVO saveSkill(@RequestHeader String token, @Valid @RequestBody Skill skill) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("已失效");
        }
        Long userId = jsonObject.getLong("id");
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        skill.setUserId(userId);
        skill.setEnterpriseId(enterpriseId);
        skill.setIsBackend(false); // 标记后台添加 否
        return skillService.saveSkill(skill);
    }

//    废除
//    @ApiOperation(value = "审核技能通过(小程序)", notes = "审核技能通过(小程序)", httpMethod = "POST", tags = "小程序")
//    @RequestMapping(value = "checkStatusSuccess/{id}", method = RequestMethod.POST)
//    public ReturnVO checkStatusSuccess(@PathVariable Long id) {
//        return skillService.checkSkill(id, 1, false);
//    }
//
//    @ApiOperation(value = "审核技能失败(小程序)", notes = "审核技能失败(小程序)", httpMethod = "POST", tags = "小程序")
//    @RequestMapping(value = "checkStatusFail/{id}", method = RequestMethod.POST)
//    public ReturnVO checkStatusFail(@PathVariable Long id) {
//        return skillService.checkSkill(id, 2, false);
//    }

    @ApiOperation(value = "根据id获取详情(小程序)", notes = "根据id获取详情(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listDetail/{id}", method = RequestMethod.POST)
    public ReturnVO<SkillVo> listDetail(@RequestHeader String token, @PathVariable Long id) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        Long userId = jsonObject.getLong("id");
        return skillService.selectSkillDetail(id, enterpriseId, userId);
    }

    @ApiOperation(value = "查询所有技能(小程序)", notes = "查询所有技能(小程序)-分页", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillVo>>> list(@RequestHeader String token, @RequestBody SkillDto skillDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        // skillDto.setCheckStatus(1); // 普通查询 小程序端 强制查询已通过的
        return skillService.listSkill(skillDto, enterpriseId, null);
    }

    @ApiOperation(value = "评论(小程序)", notes = "评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveComment", method = RequestMethod.POST)
    public ReturnVO saveComment(@RequestBody SkillComment comment, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("已失效");
        }
        comment.setIcon(jsonObject.getString("icon"));
        comment.setNickname(jsonObject.getString("nickname"));
        comment.setWxUserId(jsonObject.getLong("id"));
        return skillService.saveComment(comment);
    }

    @ApiOperation(value = "根据技能id列出所有评论(小程序)", notes = "根据技能id列出所有评论(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listComment", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillComment>>> listComment(@RequestBody SkillCommentDto skillCommentDto) {
        return skillService.listComment( skillCommentDto );
    }

    @ApiOperation(value = "删除技能(小程序)", notes = "删除技能(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public ReturnVO delete(@RequestHeader String token, @PathVariable Long id) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long userId = jsonObject.getLong("id");
        return skillService.deleteSkill(id, userId, 0L, false);
    }


    @ApiOperation(value = "查询用户发布的技能(小程序)", notes = "查询用户发布的技能(小程序)-分页", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listByUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillVo>>> listByUser(@RequestHeader String token, @RequestBody SkillDto skillDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        Long userId = jsonObject.getLong("id");
        return skillService.listSkill(skillDto, enterpriseId, userId);
    }

    @ApiOperation(value = "查询发布排名(小程序)", notes = "查询发布排名(小程序)-分页", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listRank", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillPublishVo>>> listRank(@RequestHeader String token, @RequestBody SkillPublishDto skillPublishDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillService.listSkillPublishRank(skillPublishDto, enterpriseId);
    }



}
